<template>
    <div id="ds29">
        <Breadcrumb style="margin-bottom: 10px">
            <BreadcrumbItem to="/superAdmin">首页</BreadcrumbItem>
            <BreadcrumbItem>市场推广管理</BreadcrumbItem>
            <BreadcrumbItem>推荐码管理</BreadcrumbItem>
        </Breadcrumb>

        <Input v-model="reffer" placeholder="查询推荐码" style="width: 150px"/>
        &nbsp;
        <Button type="info" @click="search()">查询</Button>
        &nbsp;
        <Button type="info" @click="addBool=true">添加</Button>

        <div style="margin-top: 10px">
            <Table :maxWidth="1000" :minWidth="10" stripe :columns="columns" :data="data"
                   v-if="data&&data.length>0"></Table>

            <Page :total="pageTotal" :current="curPage" @on-change="pageChange"
                  style="display: inline-block;padding-top: 10px;padding-bottom: 10px" show-elevator v-if="data&&data.length>0"/>
            <div style="padding-left: 10px;position:relative ;display: inline-block;height: 52px;line-height: 52px;top: -12px">
                总计 {{pageTotal}} 条数据
            </div>
        </div>
        <div id="loading" v-if="loading">
            <div class="spinner">
                <div class="double-bounce1"></div>
                <div class="double-bounce2"></div>
            </div>
        </div>

        <Modal v-model="updataBool">
            <div slot="header" style="font-size: 18px;text-align: center;color: #0ca5f5">
                修改
            </div>
            <div style="text-align:left;font-size: 16px">

                <label for="two">有效</label>
                <input type="radio" id="two" value="有效" v-model="updata.status1">
                &nbsp;
                <label for="one">无效</label>
                <input type="radio" id="one" value="无效" v-model="updata.status1">

                <br>
                <br>
                <input v-model="updata.referrer" oninput="value=value.replace(/[^\d]/g,'')" placeholder="推荐码">
                <br>
                <br>
                <input v-model="updata.name" placeholder="推荐人">
            </div>
            <div slot="footer">
                <Button type="info" @click="updataBool=false">取消</Button>
                <Button type="info" @click="updataReferrer()">确定</Button>
            </div>
        </Modal>


        <Modal v-model="addBool">
            <div slot="header" style="font-size: 18px;text-align: center;color: #0ca5f5">
                添加推荐码
            </div>
            <div style="text-align:left;font-size: 16px">
                <label for="b">有效</label>
                <input type="radio" id="b" value="有效" v-model="addStatus">
                &nbsp;
                <label for="a">无效</label>
                <input type="radio" id="a" value="无效" v-model="addStatus">
                <br>
                <br>
                <input v-model="addReferrer" oninput="value=value.replace(/[^\d]/g,'')" placeholder="推荐码">
                <br>
                <br>
                <input v-model="addName" placeholder="推荐人">
            </div>
            <div slot="footer">
                <Button type="info" @click="addBool=false">取消</Button>
                <Button type="info" @click="addRef()">确定</Button>
            </div>
        </Modal>

    </div>
</template>
<script type="text/ecmascript-6">
    import sessionUtils from '../../model/sessionUtils';
    import ipServer from '../../model/ip';
    import Axios from 'axios';
    export default{
        data(){
            return {
                addBool: false,
                addStatus: "有效",
                addReferrer: "",
                addName: "",
                reffer: "",
                loading: false,
                updata: "",
                updataBool: false,
                updataIndex: 0,
                pageTotal: 0,
                curPage: 0,
                columns: [
                    {
                        title: '创建时间',
                        key: 'createTime1',
                    }, {
                        title: '推荐码',
                        key: 'referrer',
                    },
                    {
                        title: '推荐人',
                        key: 'name',
                    },
                    {
                        title: '是否有效',
                        key: 'status1'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    }, style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.updata = {...params.row};
                                            this.updataBool = true;
                                            this.updataIndex = params.index;
                                        }
                                    }
                                }, '修改')
                                ,
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            var ds = this;
                                            this.$Modal.confirm({
                                                title: "提示",
                                                content: "确定删除?",
                                                onOk(){
                                                    ds.delData(params);
                                                },
                                                onCancel(){

                                                }
                                            });
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],
                data: []
            }
        },
        mounted(){
            this.getData();
        },
        methods: {
            search(){
                var param = new URLSearchParams();
                param.append('mobile', sessionUtils.getSession("mobile"));
                param.append('token', sessionUtils.getSession("token"));

                if (this.reffer) {
                    param.append('reffer', this.reffer + "");
                }
                this.loading = true;
                Axios.post(ipServer + "market/selectReferrerList", param)
                        .then(res => {

                            if (res.data.code == 0) {
                                sessionUtils.setSession("token", res.data.token);
                                for (var i = 0; i < res.data.data.result.length; i++) {
                                    res.data.data.result[i].createTime1 = sessionUtils.timestampToTime(res.data.data.result[i].createTime);

                                    if (res.data.data.result[i].status == 1) {
                                        res.data.data.result[i].status1 = "无效";
                                    } else {
                                        res.data.data.result[i].status1 = "有效";
                                    }
                                }
                                this.data = res.data.data.result;
                                this.pageTotal = res.data.data.dataTotal;
                                this.curPage = res.data.data.curPage;
                                if (this.data.length == 0)
                                    this.$Message.info("查询数据不存在!");
                            } else if (res.data.code == 3) {
                                sessionUtils.clear();
                                this.$Message.info(res.data.msg);
                                this.$router.push("/login");

                            } else {
                                this.$Message.info(res.data.msg);
                            }
                            this.loading = false;
                        })
                        .catch(err=> {
                            this.loading = false;
                            this.$Message.info(err);
                        });
            },
            pageChange(page){
                this.getData(page);
            },
            addRef(){
                var param = new URLSearchParams();
                param.append('mobile', sessionUtils.getSession("mobile"));
                param.append('token', sessionUtils.getSession("token"));

                if (this.addStatus == "有效") {
                    param.append('status', "0");
                }
                if (this.addStatus == "无效") {
                    param.append('status', "1");
                }
                if (this.addReferrer) {
                    param.append('referrer', this.addReferrer);
                } else {
                    this.$Message.info("请输入推荐码!");
                    return;
                }

                if (this.addName) {
                    param.append('name', this.addName);
                } else {
                    this.$Message.info("请输入推荐人!");
                    return;
                }


                this.loading = true;
                Axios.post(ipServer + "market/addReferrer", param)
                        .then(res => {
                            if (res.data.code == 0) {
                                sessionUtils.setSession("token", res.data.token);
                                this.addStatus = "有效";
                                this.addReferrer = "";
                                this.addName = "";
                                this.addBool = false;
                                this.$Message.info("添加成功!");
                                this.getData();
                            } else if (res.data.code == 3) {
                                sessionUtils.clear();
                                this.$router.push("/login");
                                this.$Message.info(res.data.msg);
                            } else {
                                this.$Message.info(res.data.msg);
                            }
                            this.loading = false;
                        })
                        .catch(err=> {
                            this.loading = false;
                            this.$Message.info(err);
                        });
            }, updataReferrer(){
                var param = new URLSearchParams();
                param.append('mobile', sessionUtils.getSession("mobile"));
                param.append('token', sessionUtils.getSession("token"));
                param.append('referrerId', this.updata._id);

                if (this.updata.status1 == "有效") {
                    param.append('status', "0");
                }
                if (this.updata.status1 == "无效") {
                    param.append('status', "1");
                }

                if (this.updata.name) {
                    param.append('name', this.updata.name);
                }

                if (this.updata.referrer) {
                    param.append('referrer', this.updata.referrer);
                }

                this.loading = true;
                Axios.post(ipServer + "market/updateReferrerById", param)
                        .then(res => {
                            if (res.data.code == 0) {
                                sessionUtils.setSession("token", res.data.token);
                                this.data[this.updataIndex].name = this.updata.name;
                                this.data[this.updataIndex].referrer = this.updata.referrer;
                                this.data[this.updataIndex].status1 = this.updata.status1;

                                if (this.updata.status1 == "有效") {
                                    this.data[this.updataIndex].status = 0;
                                }
                                if (this.updata.status1 == "无效") {
                                    this.data[this.updataIndex].status = 1;
                                }
                                this.updataBool = false;
                                this.$Message.info("修改成功!");
                            } else if (res.data.code == 3) {
                                sessionUtils.clear();
                                this.$router.push("/login");
                                this.$Message.info(res.data.msg);
                            } else {
                                this.$Message.info(res.data.msg);
                            }
                            this.loading = false;
                        })
                        .catch(err=> {
                            this.loading = false;
                            this.$Message.info(err);
                        });
            }, delData(data){
                var param = new URLSearchParams();
                param.append('mobile', sessionUtils.getSession("mobile"));
                param.append('token', sessionUtils.getSession("token"));
                param.append('referrerId', data.row._id);
                this.loading = true;

                Axios.post(ipServer + "market/removeReferrerById", param)
                        .then(res => {
                            if (res.data.code == 0) {
                                sessionUtils.setSession("token", res.data.token);
                                this.data.splice(data.index, 1);
                                this.$Message.info("删除成功!");
                            } else if (res.data.code == 3) {
                                sessionUtils.clear();
                                this.$router.push("/login");
                                this.$Message.info(res.data.msg);
                            } else {
                                this.$Message.info(res.data.msg);
                            }
                            this.loading = false;
                        })
                        .catch(err=> {
                            this.loading = false;
                            this.$Message.info(err);
                        });
            },
            getData(page){
                var param = new URLSearchParams();
                param.append('mobile', sessionUtils.getSession("mobile"));
                param.append('token', sessionUtils.getSession("token"));
                if (page) {
                    param.append('pageId', page + "");
                }
                this.loading = true;
                Axios.post(ipServer + "market/selectReferrerList", param)
                        .then(res => {
                            if (res.data.code == 0) {
                                sessionUtils.setSession("token", res.data.token);

                                console.log("打印" + JSON.stringify(res));
                                for (var i = 0; i < res.data.data.result.length; i++) {
                                    res.data.data.result[i].createTime1 = sessionUtils.timestampToTime(res.data.data.result[i].createTime);

                                    if (res.data.data.result[i].status == 1) {
                                        res.data.data.result[i].status1 = "无效";
                                    } else {
                                        res.data.data.result[i].status1 = "有效";
                                    }
                                }
                                this.data = res.data.data.result;
                                this.pageTotal = res.data.data.dataTotal;
                                this.curPage = res.data.data.curPage;
                            } else if (res.data.code == 3) {
                                sessionUtils.clear();
                                this.$router.push("/login");
                                this.$Message.info(res.data.msg);
                            } else {
                                this.$Message.info(res.data.msg);
                            }
                            this.loading = false;
                        })
                        .catch(err=> {
                            this.loading = false;
                            this.$Message.info(err);
                        });
            }
        }
    }
</script>


<style scoped>
    @import "../../assets/css/loading.css";

</style>